<template>
  <div>
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
        <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :fetchapi="fetchApi"
      :datakey="'records'"
      :onColumnsChange="(columns) => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
      columnsRef="ClaimQueryFactorylgr"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
     <BaseDialog :visible.sync="visibleAdd" :title="'选择经销商'" destroyOnClose width="850px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DealerList @close="closeAdd" @save="saveAdd"></DealerList>
    </BaseDialog>
    <BaseDialog :visible.sync="peijianVisible" :title="'祸首件选择'" width="850px" destroyOnClose  :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <peijianSelect @close="peijianClose" @save="peijianSave"></peijianSelect>
    </BaseDialog>
    <BaseDialog :visible.sync="suppliers" :title="'供应商选择'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <SupplierList @close="suppliersClose" @save="suppliersSave" ></SupplierList>
    </BaseDialog>
 </div>
</template>
<script>
import {getListByPage,getListForExport,exporturl} from '@/api/claim/factory/DealerClaim';
import { exportExcelFile } from '@/utils/index';
import peijianSelect from '@/pages/claimDemage/tools/vehicle.vue';
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { dictionary } from '@/utils/dictMixin';
import SupplierList from '@/pages/claimDemage/tools/SuppliersList.vue';
import { getClaim } from '@/api/dictionary/index';
import {  getAppId } from '@/assets/js/auth';
import moment from "moment";

export default {
   mixins: [dictionary],
  components: {
    DealerList,
    peijianSelect,
    SupplierList,
  },
  data() {
    return {
      collapse: true,
      topFilterInit: this.topFilterInitFun(),
      visibleAdd: false,
      peijianVisible: false,
      columns: this.creatHeader(),
      fetchParams: {xhrAbort: true},
      dataList: [],
      fetchApi: getListByPage,
      suppliers:false,
    };
  },
  created() {
    if (getAppId() == 'arcfox') {
      this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543',[55431001])
    }else{
      this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543')
    }
    this.chuqi();
  },
   methods: {
      async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000143' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // this.loading = true;
      // getListForExport(this.fetchParams).then((res) => {
      //   exportExcelFile(res.data, '索赔单信息');
      //   this.loading = false;
      // });
    },
    selectSuppliersCode() {
      this.suppliers = true;
    },
    suppliersClose(){
      this.suppliers = false;
    },
    suppliersSave(obj) {
      this.topFilterInit.find(x => x.fieldName == 'supCode').initialValue = obj[0].supCode;
    //   this.topFilterInit.find(x => x.fieldName == 'supName').initialValue = obj[0].supCode;
    //   this.supName = obj[0].supCode;
      //this.supId = obj[0].supId;
      this.suppliers = false;
    },
    chooseDealerNo() {
      this.visibleAdd = true;
    },peijianTurnOn() {
      this.peijianVisible = true;
    },
       saveAdd(val) {
      this.topFilterInit.find((x) => x.fieldName == 'ownerCode').initialValue = val[0].dealerCode;
      this.dealer = { ...val[0] };
      this.visibleAdd = false;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
      peijianSave(obj) {
      this.topFilterInit.find(x => x.fieldName == 'partCode').initialValue = obj[0].partCode;
      this.peijianVisible = false;
    },
    peijianClose(){
      this.peijianVisible = false;
    },
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    chooseOrderNo() {
        this.visible = true;
    },
       //搜索获取数据
    // changeHandle(val) {
    //   this.fetchParams = Object.assign({}, this.fetchParams, val,{techStatusStrs: '55131002, 55131003, 55131005,55131004',
    //     xhrAbort: false});
    //   // this.SELECT();
    // },
      changeHandle(val) {
      if(Array.isArray(val.cmsClaimTypeList)){
        val.cmsClaimTypeLists = val.cmsClaimTypeList.join(",");
      }


      // val.dealerIds = this.dealerIds.join(",");
      //this.$emit('filterChange', val)
      this.fetchParams = Object.assign({}, this.fetchParams, val, {xhrAbort: false});
      console.log(val);
    },
     topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '选择经销商',
          fieldName: 'ownerCode',
          placeholder: '请输入',
          initialValue: '',
          unitRender: (p) => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseDealerNo()} />
              </div>
            );
          },
        },
        { type: 'MULTIPLE_SELECT', label: '索赔类型', fieldName: 'cmsClaimTypeList', placeholder: '请选择', initialValue: '',
        itemList: [] },
      //  MULTIPLE_SELECT
        //  { type: 'INPUT',
        //   label: '供应商',
        //   fieldName: 'supCode',
        //   placeholder: '请选择',
        //   initialValue: '',
        //   unitRender: p => {
        //     return (
        //       <div>
        //       <multiuse-button icon="el-icon-search" onClick={() => this.selectSuppliersCode()} />
        //     </div>
        //   );
        //   }
        // },
        { type: 'INPUT', label: '索赔单号', fieldName: 'claimNo', placeholder: '请输入', initialValue: '' },
        {
          type: 'INPUT',
          label: '主损件代码',
          fieldName: 'partCode',
          placeholder: '请输入',
          initialValue: '',
          unitRender: (p) => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.peijianTurnOn()} />
              </div>
            );
          },
        },
        { type: 'INPUT', label: '主损件名称', fieldName: 'partName', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '车型名称', fieldName: 'modelName', placeholder: '请输入', initialValue: '' },
        { type: 'RANGE_DATE', label: '结算单生成时间', fieldName: 'beginSettleAt|endSettleAt', placeholder: '请输入',initialValue: '' },
        { type: 'SELECT', label: '索赔车辆属性', fieldName: 'ifComm', placeholder: '请选择索赔车辆属性...', initialValue: '',itemList: [] }
        // { type: 'RANGE_DATE', label: '保修日期', fieldName: 'repairBeginDate|repairEndDate', placeholder: '请输入', initialValue: '' },
        // { type: 'RANGE_DATE', label: '维修日期', fieldName: 'serviceBeginDate|serviceEndDate', placeholder: '请输入', initialValue: [
        //   moment()
        //     .startOf('week')
        //     .format('YYYY-MM-DD HH:mm:ss'),
        //     moment()
        //       .endOf('week')
        //       .format('YYYY-MM-DD HH:mm:ss')
        //   ]},
      ];
    },
    async chuqi() {
    const res2 = await getClaim();
      if (res2.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'cmsClaimTypeList').itemList = res2.data.map(y => ({ text: y.name, value: y.code }));
      }
    },
        //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },

        { title: '索赔类型', dataIndex: 'cmsClaimType', sorter: true },
        { title: '索赔二级分类', dataIndex: 'batchName', sorter: true },
        { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
        { title: '经销商代码', dataIndex: 'ownerCode', sorter: true },
        { title: '经销商名称', dataIndex: 'dealerName', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '保修起始日期', dataIndex: 'repairBeginDate', sorter: true },
        { title: '下线工厂', dataIndex: 'factName',sorter: true},
        { title: '车型名称', dataIndex: 'modelName', sorter: true },
        { title: '行驶里程', dataIndex: 'driveMile', sorter: true },
        { title: '故障现象代码', dataIndex: 'faultCode', sorter: true },
        { title: '故障现象描述', dataIndex: 'faultName', sorter: true },
        { title: '配件代码', dataIndex: 'partCodes', sorter: true },
        { title: '配件名称', dataIndex: 'partNames', sorter: true },
        { title: '更换件数量', dataIndex: 'partCount', sorter: true },
        { title: '更换件金额', dataIndex: 'cmsClaimPrice', sorter: true },
        { title: '更换件总金额', dataIndex: 'partAmount', sorter: true },
        { title: '故障件运费', dataIndex: 'gzjyf', sorter: true },
        { title: '维修工时金额', dataIndex: 'labourAmount', sorter: true },
        { title: '其他费用金额', dataIndex: 'addAmount', sorter: true },
        { title: '主损件代码', dataIndex: 'partCode', sorter: true },
        { title: '主损件名称', dataIndex: 'partName', sorter: true },
        { title: '主损件供应商代码', dataIndex: 'supCode', sorter: true },
        { title: '主损件供应商名称', dataIndex: 'supName', sorter: true },
        { title: '电机号', dataIndex: 'engineNo', sorter: true },
        { title: '故障现象', dataIndex: 'faultAppear', sorter: true },
        { title: '故障原因', dataIndex: 'faultReason', sorter: true },
        { title: '维修方案', dataIndex: 'cmsClaimPgm', sorter: true },
        { title: '生产日期', dataIndex: 'prodDate', sorter: true },
        { title: '结算单生成日期', dataIndex: 'settleDate', sorter: true },
        { title: '是否一口价', dataIndex: 'ifPrice', sorter: true },
        { title: '索赔总金额', dataIndex: 'claimSumAmount', sorter: true },
        { title: '维修开始日期', dataIndex: 'serviceBeginDate', sorter: true },

      ];
    },
   }
}
</script>
